﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Web.API.IBLL;
using Web.API.IDAL;
using Web.API.Model;

namespace Web.API.BLL
{
    public class MemberBll:MemberIBLL
    {
        public MemberBll(MemberIDAL<MemberModel> dal) 
        {
            Dal = dal;
        }
        public MemberIDAL<MemberModel> Dal { get;}

        public Task<int> AddMember(MemberModel member)
        {
           return Dal.AddAsync(member);
        }

        public Task<int> DeleteMember(int id)
        {
            var m = Dal.GetAllAsync().FirstOrDefault(x=>x.Id==id);
            m.Isdelete = false;
            return Dal.UpdateAsync(m);


        }

        public MemberModel Fantian(int id)
        {
            return Dal.GetAllAsync().FirstOrDefault(x=>x.Id==id);
        }

        public dynamic GetMember(string? name, string? addr)
        {
            var list = Dal.GetAllAsync().Where(x=>x.Isdelete==true);
            if (!string.IsNullOrEmpty(name))
            {
                list = list.Where(x=>x.Name.Contains(name));
            }
            if (!string.IsNullOrEmpty(addr))
            {
                list = list.Where(x=>x.Addr.Contains(addr));
            }
            return list.ToList();
        }

        public Task<int> UpdateMember(MemberModel member)
        {
           return Dal.UpdateAsync(member);
        }
    }
}
